package com.yidu.labyrinth.service;

import com.yidu.labyrinth.dao.LabyrinthRoleDao;
import com.yidu.labyrinth.main.JsonM;
import com.yidu.labyrinth.main.LabyrinthModuleEntity;
import com.yidu.labyrinth.main.LabyrinthModuleOrRoleEntity;
import com.yidu.labyrinth.main.LabyrinthRoleEntity;
import com.yidu.labyrinth.vo.LabyrinthVo;
import com.yidu.labyrinth.vo.Pages;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @program: labyrinth
 * @description 角色service实现类
 * @Date:2019-09-25 13:48
 * @Author:谭俊
 **/
public class LabyrinthRoleServiceImpl implements LabyrinthRoleService {


    private LabyrinthRoleDao roleDao;


    public LabyrinthRoleDao getRoleDao() {
        return roleDao;
    }

    public void setRoleDao(LabyrinthRoleDao roleDao) {
        this.roleDao = roleDao;
    }

    /**
     * 查询所有
     * @param labyrinthVo vo对象
     * @param pages  分页
     * @return
     */
    @Override
    public List<LabyrinthVo> findAll(LabyrinthVo labyrinthVo, Pages pages) {

        //创建一个po对象
        LabyrinthRoleEntity labyrinthPo = new LabyrinthRoleEntity();

        BeanUtils.copyProperties(labyrinthVo,labyrinthPo);


        List<LabyrinthRoleEntity> list = roleDao.findAll(labyrinthPo,pages);



         List<LabyrinthVo> listVo = new ArrayList<LabyrinthVo>();


        for (Iterator<LabyrinthRoleEntity> iterator = list.iterator(); iterator.hasNext(); ) {

            LabyrinthRoleEntity po =  iterator.next();

            LabyrinthVo vo = new LabyrinthVo();

            BeanUtils.copyProperties(po,vo);
            System.out.println("名字"+vo.getRoleName());

            listVo.add(vo);

        }

        return listVo;
    }


    /**
     * 查询行数
     * @param labyrinthVo Vo模型层
     * @return
     */
    @Override
    public Long count(LabyrinthVo labyrinthVo) {


        return roleDao.count(labyrinthVo);

    }

    /**
     * vo转换po
     * @param vo
     * @return
     */
    private LabyrinthRoleEntity addOrUpdate(LabyrinthVo vo){
        LabyrinthRoleEntity po =new LabyrinthRoleEntity();
        BeanUtils.copyProperties(vo,po);
        return po;
    }


    /**
     * 修改状态
     * @param vo
     * @return
     */
    @Override
    public int save(LabyrinthVo vo) {

        LabyrinthRoleEntity labyrinthRoleEntity = roleDao.findById(vo.getRoleId());

        labyrinthRoleEntity.setRoleResult(vo.getRoleResult());

        return roleDao.save(labyrinthRoleEntity);
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public LabyrinthVo findById(String id) {

        LabyrinthRoleEntity labyrinthRoleEntity=roleDao.findById(id);


        LabyrinthVo vo=new LabyrinthVo();

        BeanUtils.copyProperties(labyrinthRoleEntity,vo);

        return vo;

    }





    @Override
    public List<LabyrinthVo> findAlls() {

        System.out.println("弄死你了");
        List<LabyrinthModuleEntity> list = roleDao.findAlls();
        System.out.println("弄死你"+list.get(0).getModuleId());
        List<LabyrinthVo> listVo = new ArrayList<LabyrinthVo>();
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            LabyrinthModuleEntity module = (LabyrinthModuleEntity) iterator.next();
            LabyrinthVo moduleVo = new LabyrinthVo();

            BeanUtils.copyProperties(module, moduleVo);

            listVo.add(moduleVo);

            System.out.println(moduleVo.toString()+"集合");
        }

        return listVo;

    }




}
